﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;

namespace SimplePMT.Components
{
    public static class FormElement
    {
        public static string TextBox(int size, string id, string value, string optional, bool edit)
        {
            if (!edit)
            {
                return value;
            }
            else
            {
                return "<input type=\"text\" size=\""+ size +"\" name=\"" + id + "\" id=\"" + id + "\" value=\"" + value + "\" " + optional + "/>";
            }
        }
        public static string TextArea(int cols, int rows, string id, string value, string optional, bool edit)
        {
            if (!edit)
            {
                return Common.ConvertToHTML(value);
            }
            else
            {
                return "<textarea cols=\""+ cols +"\" rows=\""+ rows +"\" name=\"" + id + "\" id=\"" + id + "\" " + optional + ">" + value + "</textarea>";
            }
        }
        public static string CheckBox(string id, string value, string valueText, string optional, bool edit, bool selected)
        {
            if (!edit)
            {
                string[] values = valueText.Split("|".ToCharArray());
                if (value.Equals("1"))
                {
                    return values[0];
                }
                else
                {
                    return values[1];
                }
            }
            else
            {
                string selectedText = "";
                if (selected)
                {
                    selectedText = " checked";
                }
                return "<input type=\"checkbox\" id=\"" + id + "\" name=\"" + id + "\" value=\"" + value + "\" " + optional + selectedText + " />";
            }
        }

        public static string CheckBoxDisplay(string id, string value, string valueText, string optional, bool edit, bool selected)
        {
            string[] values = valueText.Split("|".ToCharArray());
            if (values.Length < 2)
            {
                values = new string[] { "", valueText };
            }
            if (!edit)
            {
                
                if (value.Equals("1"))
                {
                    return values[0];
                }
                else
                {
                    return values[1];
                }
            }
            else
            {
                string selectedText = "";
                if (selected)
                {
                    selectedText = " checked";
                }
                return "<input type=\"checkbox\" id=\"" + id + "\" name=\"" + id + "\" value=\"" + value + "\" " + optional + selectedText + " />&nbsp;" + values[1];
            }
        }

        public static string Select(int sizeInPx, string id, int selectedId, string[] options, string optional, bool edit)
        {
            if (!edit)
            {
                string value = "Not Specified";
                foreach (string keyValuePair in options)
                {
                    string[] kvSplit = keyValuePair.Split("=".ToCharArray());
                    if (Convert.ToInt32(kvSplit[0]) == selectedId)
                    {
                        value = kvSplit[1];
                    }
                }

                return value;
            }
            else
            {
                StringBuilder list = new StringBuilder();
                list.Append("<select id=\"" + id + "\" name=\"" + id + "\" style=\"width:" + sizeInPx + "px\" " + optional + ">\n");
                foreach (string keyValuePair in options)
                {
                    string[] kvSplit = keyValuePair.Split("=".ToCharArray());
                    list.Append("<option value=\"" + kvSplit[0] + "\"");
                    if (Convert.ToInt32(kvSplit[0]) == selectedId)
                    {
                        list.Append(" selected");
                    }
                    list.Append(">" + kvSplit[1] + "</option>\n");
                }
                list.Append("</select>\n");
                return list.ToString();
            }
        }

        public static string Select(int sizeInPx, string id, int selectedId, List<string> options, string optional, bool edit)
        {
            if (!edit)
            {
                string value = "Not Specified";
                foreach (string keyValuePair in options)
                {
                    string[] kvSplit = keyValuePair.Split("=".ToCharArray());
                    if (Convert.ToInt32(kvSplit[0]) == selectedId)
                    {
                        value = kvSplit[1];
                    }
                }

                return value;
            }
            else
            {
                StringBuilder list = new StringBuilder();
                list.Append("<select id=\"" + id + "\" name=\"" + id + "\" style=\"width:" + sizeInPx + "px\" " + optional + ">\n");
                foreach (string keyValuePair in options)
                {
                    string[] kvSplit = keyValuePair.Split("=".ToCharArray());
                    list.Append("<option value=\"" + kvSplit[0] + "\"");
                    if (Convert.ToInt32(kvSplit[0]) == selectedId)
                    {
                        list.Append(" selected");
                    }
                    list.Append(">" + kvSplit[1] + "</option>\n");
                }
                list.Append("</select>\n");
                return list.ToString();
            }
        }
    }
}
